﻿Imports System.Data.OleDb
Public Class BeachesByTag
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request.Params("Tag") = "" Then Response.Redirect("BeachesList.aspx")
        myHeading.InnerHtml = Request.Params("Tag")
    End Sub

    Protected Sub btn_findByName_Click(sender As Object, e As EventArgs) Handles btn_findByName.Click
        searchDatabase("BeachName", tb_search.Text)
    End Sub

    Protected Sub btn_findByRegion_Click(sender As Object, e As EventArgs) Handles btn_findByRegion.Click
        searchDatabase("Region", tb_search.Text)
    End Sub

    Private Function searchDatabase(ByVal buttonStr As String, ByVal searchStr As String)
        SqlDataSource2.SelectCommand = "SELECT DISTINCT * FROM BeachTagView WHERE Tag = @f1 AND " & buttonStr & " LIKE '%" & searchStr & "%'"
        SqlDataSource2.SelectParameters.Add("@f1", Request.Params("Tag"))
        SqlDataSource2.DataBind()
        Return True
    End Function



    Protected Sub SqlDataSource1_Selected(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource2.Selected
        If (e.AffectedRows < 1) Then
            myHeading.InnerHtml = "No Beach found"
        End If
        If (e.AffectedRows < 9) Then
            DataPagerBeaches.Visible = False
        Else
            DataPagerBeaches.Visible = True
        End If
    End Sub

    Protected Sub ListView1_DataBound(sender As Object, e As EventArgs)
        'Display datapager only if there is more than one page to display
        DataPagerBeaches.Visible = (DataPagerBeaches.PageSize < DataPagerBeaches.TotalRowCount)
    End Sub

    Protected Function getViews(ByVal beachID As Integer) As String
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "SELECT * FROM TotalViewsForAllBeaches WHERE ID = @f1"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        oleDbConn.Open()
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", beachID)
        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        dr.Read()
        Return dr("TotalViews")
        oleDbConn.Close()
    End Function
End Class